# -------------------------------------------------------------
# Downloading Renewable production by energy source from EIA
# Need EIA API key in order to run this script, saved as 
# EIA_KEY as an environment variable
# -------------------------------------------------------------
library(pacman)
p_load(
  eia, data.table, here, fst, tidyverse
)

# 3390101 is the net generation by energy source code
nges_cat = eia_cats(3390101)

# Now fitlering to just the balancing authorities
nges_ba_cat = 
  nges_cat$childcategories |>
  filter(!(category_id %in% 3390105:3390118))

nges_region_cat = 
  nges_cat$childcategories |>
  filter(category_id %in% 3390106:3390118)


# Collecting all of the series IDs
nges_ba_series = 
  map_dfr(
    nges_ba_cat$category_id,
    \(id){eia_cats(id)$childseries}
  )
nges_region_series = 
  map_dfr(
    nges_region_cat$category_id,
    \(id){eia_cats(id)$childseries}
  )

nges_ba_series_H = filter(nges_ba_series, f == "H")
nges_region_series_H = filter(nges_region_series, f == "H")

nges_ex_series_HL = nges_region_series$series_id[2]
ex_series = eia_series(id = nges_ex_series_HL)

# Making the API call
nges_ba_raw = 
  map_dfr(
    seq(1, nrow(nges_ba_series_H), by = 20),
    \(i){
      eia_series(
        id = nges_ba_series_H$series_id[i:min(i+19, nrow(nges_ba_series_H))]
      )
    }
  ) |> 
  unnest(cols = data) |>
  setDT() %>%
  .[,.(
    series_id, 
    ba = str_extract(series_id, "(?<=EBA\\.)[:alpha:]+(?=-)"),
    gen_type = str_extract(series_id, "(?<=NG\\.)[:alpha:]+(?=\\.H)"),
    units,
    date, year, month, week, 
    value
  )][,':='(
    gen_type = fcase(
      gen_type == "COL", "coal",
      gen_type == "NG" , "gas",
      gen_type == "NUC", "nuclear",
      gen_type == "OIL", "oil",
      gen_type == "OTH", "other",
      gen_type == "SUN", "solar",
      gen_type == "WAT", "hydro",
      gen_type == "WND", "wind"
    )
  )]       

nges_region_raw = 
  map_dfr(
    seq(1, nrow(nges_region_series_H), by = 20),
    \(i){
      eia_series(
        id = nges_region_series_H$series_id[i:min(i+19, nrow(nges_region_series_H))]
      )
    }
  ) |> 
  unnest(cols = data) |>
  setDT() %>%
  .[,.(
    series_id, 
    region = str_extract(series_id, "(?<=EBA\\.)[:alpha:]+(?=-)"),
    gen_type = str_extract(series_id, "(?<=NG\\.)[:alpha:]+(?=\\.H)"),
    units,
    date, year, month, week, 
    value
  )][,':='(
    gen_type = fcase(
      gen_type == "COL", "coal",
      gen_type == "NG" , "gas",
      gen_type == "NUC", "nuclear",
      gen_type == "OIL", "oil",
      gen_type == "OTH", "other",
      gen_type == "SUN", "solar",
      gen_type == "WAT", "hydro",
      gen_type == "WND", "wind"
    )
  )]

# Saving the results 
write.fst(
  nges_ba_raw,
  here("Data/electricity-generation/eia-nges/eia-nges-ba-raw.fst")
)
write.fst(
  nges_region_raw,
  here("Data/electricity-generation/eia-nges/eia-nges-region-raw.fst")
)




